package com.app.server.impl;

import com.app.common.Result;
import com.app.mapper.RecordFileMapper;
import com.app.mapper.RescueHandMapper;
import com.app.mapper.UnitsRescueMapper;
import com.app.model.RecordFileModel;
import com.app.model.RescueHandleModel;
import com.app.model.UnitsRescueModel;
import com.app.server.RescueHandService;
import com.app.utils.CommonHelp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RescueHandServiceImpl extends ServiceImpl<RescueHandMapper, RescueHandleModel> implements RescueHandService {
    @Autowired
    private RescueHandMapper rescueHandMapper;

    @Autowired
    private RecordFileMapper recordFileMapper;

    @Autowired
    private UnitsRescueMapper unitsRescueMapper;

    @Override
    public Result<String> addAndUpdateHandle(RescueHandleModel rescueHandleModel) {
        RescueHandleModel rescueHandle = rescueHandMapper.selectById(rescueHandleModel.getId());
        if (rescueHandle != null) {
            rescueHandleModel.setUpdateTime(CommonHelp.getNowTimestamp());
            rescueHandMapper.updateById(rescueHandleModel);
            return Result.success("修改成功");
        } else {
            QueryWrapper<RescueHandleModel> qw = new QueryWrapper<>();
            qw.eq("rescue_id", rescueHandleModel.getRescueId());
            RescueHandleModel handle = rescueHandMapper.selectOne(qw);
            if (handle != null) {
                return Result.success("已处理");
            }
            rescueHandleModel.setCreateTime(CommonHelp.getNowTimestamp());

            StringBuilder s1 = new StringBuilder();
            StringBuilder s2 = new StringBuilder();
            if (rescueHandleModel.getPic() != null && rescueHandleModel.getPic().length > 0) {
                String[] pic = rescueHandleModel.getPic();
                for (int i = 0; i < pic.length; i++) {
                    String[] split = pic[i].split("/");
                    int length = split.length;
                    RecordFileModel recordFileModel = new RecordFileModel();
                    recordFileModel.setName(split[length - 1]);
                    recordFileModel.setUrl(pic[i]);
                    recordFileModel.setFileType(1);
                    recordFileModel.setCreateTime(CommonHelp.getNowTimestamp());
                    recordFileMapper.insert(recordFileModel);
                    if (i == pic.length - 1) {
                        s1.append(recordFileModel.getId());
                    } else {
                        s1.append(recordFileModel.getId()).append(",");
                    }
                }
            }
            if (rescueHandleModel.getVideo() != null && rescueHandleModel.getVideo().length > 0) {
                String[] pic = rescueHandleModel.getVideo();
                for (int i = 0; i < pic.length; i++) {
                    String[] split = pic[i].split("/");
                    int length = split.length;
                    RecordFileModel recordFileModel = new RecordFileModel();
                    recordFileModel.setName(split[length - 1]);
                    recordFileModel.setUrl(pic[i]);
                    recordFileModel.setFileType(2);
                    recordFileModel.setCreateTime(CommonHelp.getNowTimestamp());
                    recordFileMapper.insert(recordFileModel);
                    if (i == pic.length - 1) {
                        s2.append(recordFileModel.getId());
                    } else {
                        s2.append(recordFileModel.getId()).append(",");
                    }
                }
            }
            if (s1.length() > 0 && s2.length() > 0) {
                rescueHandleModel.setFileId(s1 + "," + s2);
            }
            if (s1.length() > 0 && s2.length() == 0) {
                rescueHandleModel.setFileId(s1.toString());
            }
            if (s2.length() > 0 && s1.length() == 0) {
                rescueHandleModel.setFileId(s2.toString());
            }
            rescueHandMapper.insert(rescueHandleModel);
            return Result.success("添加成功");
        }
    }
}
